package com.pikaqiu.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pikaqiu.warehouse.domain.common.ReqCommonParams;
import com.pikaqiu.warehouse.domain.dto.SpecialUserDto;
import com.pikaqiu.warehouse.domain.entity.Supplier;
import com.pikaqiu.warehouse.domain.req.SpecialUserForm;
import com.pikaqiu.warehouse.mapper.SupplierMapper;
import com.pikaqiu.warehouse.service.SupplierService;
import com.pikaqiu.warehouse.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @createDate 2024-08-16 20:41:38
*/
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier>
    implements SupplierService{

    @Resource
    private UserService userService;

    @Override
    public List<SpecialUserDto> listCompanySupplier() {
        ReqCommonParams reqCommonParams = userService.checkUser();
        Integer companyId = reqCommonParams.getCompanyId();
        List<Supplier> supplierList = list(new QueryWrapper<Supplier>().eq("company_id", companyId));
        return supplierList.stream().map(SpecialUserDto::new).collect(Collectors.toList());
    }

    @Override
    public void saveSupplier(SpecialUserForm specialUserForm) {
        ReqCommonParams reqCommonParams = userService.checkUser();
        Supplier supplier = specialUserForm.toSupplier();
        supplier.setCompanyId(reqCommonParams.getCompanyId());
        if (specialUserForm.getId() != null) {
            supplier.setUpdateBy(reqCommonParams.getUserId().toString());
        } else {
            supplier.setCreateBy(reqCommonParams.getUserId().toString());
        }
        save(supplier);
    }
}




